package com.cx.bank.view;

import com.cx.bank.dao.BankDaoImpl;
import com.cx.bank.dao.BankDaoInterface;
import com.cx.bank.manager.ManagerImpl;
import com.cx.bank.manager.managerInterface;
import com.cx.bank.model.MoneyBean;
import com.cx.bank.model.UserBean;
import com.cx.bank.util.AccountOverDrawnException;
import com.cx.bank.util.InvalidDepositException;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Scanner;

/**
 * @description:实现银行管理的GUI页面
 * @author:gong
 * @create: 13/7/2022 下午3:14
 */
public class MyView {

    public static void main(String[] args) {
        UserBean userBean = new UserBean();
        MyView loginView = new MyView();
        loginView.createView();
    }

    public MyView() {
    }

    public void createView(){
        //登录页面
        managerInterface managerInterface = ManagerImpl.getInstance();
        JFrame jf = new JFrame("首页");
        jf.setLayout(new FlowLayout(FlowLayout.CENTER));
        jf.setSize(500,500);   //大小
        jf.setLocation(700,300);		//位置
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel jp = new JPanel();
        jp.setLayout(new FlowLayout());
        jp.setBorder(new EmptyBorder(100, 100, 100, 100));
        JTextField jTextField1 = new JTextField(20);
        JTextField jTextField2 = new JTextField(19);
        JLabel jLabel1 = new JLabel("用户名");
        JLabel jLabel2 = new JLabel("密码");
        JButton jButton1 = new JButton("登录");
        JButton jButton2 = new JButton("注册");
        jp.add(jLabel1);
        jp.add(jTextField1);
        jp.add(jLabel2);
        jp.add(jTextField2);
        jp.add(jButton1);
        jp.add(jButton2);
        jf.setContentPane(jp);
        jf.setVisible(true);

        //点击登录按钮获取表单的值
        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取文本框中的值
                String username = jTextField1.getText();
                System.out.println(username);
                String pwd = jTextField2.getText();
                System.out.println(pwd);
                boolean flag = managerInterface.login(username, pwd);
                if (flag) {
                    verifyIdentity(username, pwd);
                }else {
                    //登录失败（待处理）设置弹窗

                    JOptionPane.showMessageDialog(null, "用户名或密码错误.", "标题",JOptionPane.ERROR_MESSAGE);
                }

            }
        });
        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jp.setVisible(false);
                registerView(jf,jp);
            }
        });

    }
    public void registerView(JFrame jf,JPanel jp){
        //注册页面
        managerInterface managerInterface = ManagerImpl.getInstance();
        JPanel jp1 = new JPanel();
        jp1.setLayout(new FlowLayout());
        jp1.setBorder(new EmptyBorder(100, 100, 100, 100));
        JTextField jTextField3 = new JTextField(20);
        JTextField jTextField4 = new JTextField(19);
        JLabel jLabel3 = new JLabel("用户名");
        JLabel jLabel4 = new JLabel("密码");
        JButton jButton3 = new JButton("确认");
        JButton jButton4 = new JButton("返回");
        jp1.add(jLabel3);
        jp1.add(jTextField3);
        jp1.add(jLabel4);
        jp1.add(jTextField4);
        jp1.add(jButton3);
        jp1.add(jButton4);
        jf.setContentPane(jp1);
        jf.setVisible(true);
        jButton3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取文本框中的值
                String username = jTextField3.getText();
                System.out.println(username);
                String pwd = jTextField4.getText();
                System.out.println(pwd);
                //此处调用注册的方法
                try {
                    managerInterface.register(username,pwd);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
        jButton4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jp1.setVisible(false);
                jf.setContentPane(jp);
                jp.setVisible(true);
            }
        });
    }
    public void verifyIdentity(String username,String pwd){

        //登录成功，执行
        showMainView(username);

    }
    public void showMainView(String username){
        MoneyBean account = new MoneyBean();
        BankDaoInterface bankDaoInterface = BankDaoImpl.getInstance();
        bankDaoInterface.getMoney(username);
        managerInterface manager = ManagerImpl.getInstance();

        //创建页面
        JFrame jFrame = new JFrame("主界面");
        jFrame.setLayout(new FlowLayout(FlowLayout.CENTER));
        jFrame.setSize(500,500);   //大小
        jFrame.setLocation(700,300);		//位置
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel jPanel = new JPanel(new GridLayout(6, 1, 20, 20));
        JLabel jLabel = new JLabel("欢迎来到龚俊豪银行管理系统");
        JButton jButton1 = new JButton("查询余额");
        JButton jButton2 = new JButton("存款");
        JButton jButton3 = new JButton("取款");
        JButton jButton4 = new JButton("转账");
        JButton jButton5 = new JButton("退出");
        jPanel.add(jLabel);
        jPanel.add(jButton1);
        jPanel.add(jButton2);
        jPanel.add(jButton3);
        jPanel.add(jButton4);
        jPanel.add(jButton5);
        jFrame.setContentPane(jPanel);
        jFrame.setVisible(true);
        //查询余额
        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                manager.inquiry(username,account);
                double mymoney = bankDaoInterface.getMoney(username);
                JOptionPane.showMessageDialog(null, "我的余额为"+mymoney);
            }
        });
        //存款
        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //设置弹窗
                JDialog jDialog = new JDialog();
                JPanel jPanel1 = new JPanel();
                JLabel jLabel1 = new JLabel("请输入金额");
                JTextField jTextField = new JTextField(10);
                JButton jButton = new JButton("确认");
                jPanel1.add(jLabel1);
                jPanel1.add(jTextField);
                jPanel1.add(jButton);
                jDialog.setContentPane(jPanel1);
                jDialog.setBounds(300, 300, 300, 200);
                jDialog.setVisible(true);
                System.out.println("存款");
//                manager.deposit(account,)
                jButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {

                        String text = jTextField.getText();
                        Double money = Double.parseDouble(text.toString());
                        try {
                            manager.deposit(account,money,username);
                        } catch (InvalidDepositException ex) {
                            ex.printStackTrace();
                        }
                        System.out.println(money);
                        jDialog.setVisible(false);
                    }
                });
            }
        });
        //取款
        jButton3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JDialog jDialog = new JDialog();
                JPanel jPanel1 = new JPanel();
                JLabel jLabel1 = new JLabel("请输入金额");
                JTextField jTextField = new JTextField(10);
                JButton jButton = new JButton("确认");
                jPanel1.add(jLabel1);
                jPanel1.add(jTextField);
                jPanel1.add(jButton);
                jDialog.setContentPane(jPanel1);
                jDialog.setBounds(300, 300, 300, 200);
                jDialog.setVisible(true);
                System.out.println("取款");
                jButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String text = jTextField.getText();
                        Double money = Double.parseDouble(text.toString());
                        try {
                            manager.wirhDrawals(account,money,username);
                        } catch (AccountOverDrawnException ex) {
                            ex.printStackTrace();
                        } catch (InvalidDepositException ex) {
                            ex.printStackTrace();
                        }
                        jDialog.setVisible(false);
                    }
                });
            }
        });
        //转账
        jButton4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //设置弹窗
//                manager.transfer()
                JDialog jDialog = new JDialog();
                JPanel jPanel1 = new JPanel();
                JLabel jLabel1 = new JLabel("请输入金额");
                JTextField jTextField = new JTextField(10);
                JLabel jLabel2 = new JLabel("请输入转账的用户名");
                JTextField jTextField1 = new JTextField(10);
                JButton jButton = new JButton("确认");
                jPanel1.add(jLabel2);
                jPanel1.add(jTextField1);
                jPanel1.add(jLabel1);
                jPanel1.add(jTextField);
                jPanel1.add(jButton);
                jDialog.setContentPane(jPanel1);
                jDialog.setBounds(300, 300, 300, 200);
                jDialog.setVisible(true);
                System.out.println("取款");
                System.out.println("转账");
                jButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String text = jTextField.getText();
                        Double money = Double.parseDouble(text.toString());
                        String text1 = jTextField1.getText();
                        try {
                            manager.wirhDrawals(account,money,username);
                        } catch (AccountOverDrawnException ex) {
                            ex.printStackTrace();
                        } catch (InvalidDepositException ex) {
                            ex.printStackTrace();
                        }
                        manager.transfer(text1,money);
                        jDialog.setVisible(false);
                    }
                });
            }
        });
        //退出
        jButton5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }
}
